package framework.common.baseui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

import butterknife.ButterKnife;
import framework.common.Constant;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.ui.anount.LoginActivity;
import hk.m4s.lr.repair.test.ui.user.NoticeActivity;

public class UeBaseActivity extends AppCompatActivity implements TakePhoto.TakeResultListener,InvokeListener {
    private static final String TAG = TakePhotoActivity.class.getName();
    private TakePhoto takePhoto;
    private InvokeParam invokeParam;
    public Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        getTakePhoto().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        mContext=this;
        setContentView(R.layout.baselayout);

        registerBoradcastReceiver();
    }


    public void baseSetContentView(int layoutResId) {
        LinearLayout llContent = (LinearLayout) findViewById(R.id.content);
        LayoutParams params = new LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(layoutResId, null);
        llContent.addView(v, params);
    }

    public ImageView getGoBackBtn(){
        return (ImageView)findViewById(R.id.back);
    }
    public ImageView getDeleteBtn(){
        return (ImageView)findViewById(R.id.delete);
    }
    public ImageView getDeleteFootBtn(){
        return (ImageView)findViewById(R.id.deleteGoods);
    }
    public ImageView getShare(){
        return (ImageView)findViewById(R.id.share);
    }
    public ImageView getSqcan(){
        return (ImageView)findViewById(R.id.qScan);
    }
    public ImageView getCarBtn(){
        return (ImageView)findViewById(R.id.shopCarBtn);
    }
    public TextView getTitleView(){
        return (TextView)findViewById(R.id.title);
    }
    public TextView getTakeTookit(){
        return (TextView)findViewById(R.id.takeTookit);
    }
    public void setTitleText(String title){
        ((TextView)findViewById(R.id.title)).setText(title);
    }

    public void setRightText(String str){
        ((TextView)findViewById(R.id.right)).setText(str);
        ((TextView)findViewById(R.id.right)).setVisibility(View.VISIBLE);
    }

    public TextView getRightTextView(){
        return ((TextView)findViewById(R.id.right));
    }

    public void showGoBackBtn(){
        getGoBackBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UeBaseActivity.this.finish();
            }
        });
    }
    public void hiddenDeleteBtn(){
        getDeleteBtn().setVisibility(View.GONE);
    }

    public void showDelete(){
        getDeleteBtn().setVisibility(View.VISIBLE);
    }
    public void hiddenGoBackBtn(){
        getGoBackBtn().setVisibility(View.GONE);
    }

    public void showHeader(){
        ((LinearLayout)findViewById(R.id.title_bar)).setVisibility(View.VISIBLE);
    }

    public void hiddenHeader(){
        ((LinearLayout)findViewById(R.id.title_bar)).setVisibility(View.GONE);
    }
    public void hiddenNewMessage(){}
    public void showFooter(){
        ((LinearLayout)findViewById(R.id.footerbar)).setVisibility(View.VISIBLE);
    }

    public void hiddenFooter(){
        ((LinearLayout)findViewById(R.id.footerbar)).setVisibility(View.GONE);
    }

    public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("hk.m4s.lr.repair.test_finish".equals(intent.getAction())) {
                SharedPreferencesUtils.addgetSharedPreferences(Constant.token,"");
                SharedPreferencesUtils.addgetSharedPreferences(Constant.face,"");
                Intent in=new Intent(UeBaseActivity.this, LoginActivity.class);
                startActivity(in);
                finish();
            }
        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("hk.m4s.lr.repair.test_finish");
        // 注册广播
        registerReceiver(mFinishReceiver, myIntentFilter);
    }

    public void unregisterBoradcastReceiver() {
        unregisterReceiver(mFinishReceiver);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterBoradcastReceiver();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        getTakePhoto().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type=PermissionManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
        PermissionManager.handlePermissionsResult(this,type,invokeParam,this);
    }

    /**
     *  获取TakePhoto实例
     * @return
     */
    public TakePhoto getTakePhoto(){
        if (takePhoto==null){
            takePhoto= (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
        }
        return takePhoto;
    }
    @Override
    public void takeSuccess(TResult result) {
        Log.i(TAG,"takeSuccess：" + result.getImage().getCompressPath());
    }
    @Override
    public void takeFail(TResult result,String msg) {
        Log.i(TAG, "takeFail:" + msg);
    }
    @Override
    public void takeCancel() {
        Log.i(TAG, getResources().getString(R.string.msg_operation_canceled));
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type=PermissionManager.checkPermission(TContextWrap.of(this),invokeParam.getMethod());
        if(PermissionManager.TPermissionType.WAIT.equals(type)){
            this.invokeParam=invokeParam;
        }
        return type;
    }
    protected void gone(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }
    protected void visible(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }

    }

}

